﻿using Newtonsoft.Json;
using QWManage.Model;
using QWManage.Utils;
using System.Collections.Generic;

namespace QWManage.API.GroupChatDetail92122
{
    /// <summary>
    /// 获取客户群详情
    /// 通过客户群ID，获取详情。包括群名、群成员列表、群成员入群时间、入群方式。（客户群是由具有客户群使用权限的成员创建的外部群）
    /// </summary>
    public class GroupChatDetailAPI
    {
        /*
        请求方式：POST（HTTPS）
        请求地址：https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get?access_token=ACCESS_TOKEN
        参数说明：
        {
            "chat_id":"wrOgQhDgAAMYQiS5ol9G7gK9JVAAAA",
	        "need_name" : 1
        }

        参数	必须	说明
        access_token	是	调用接口凭证
        chat_id	是	客户群ID
        need_name	否	是否需要返回群成员的名字group_chat.member_list.name。0-不返回；1-返回。默认不返回
         */


        public static List<CustomerGroupInfo> InvokePlus(string token, GroupChatDetailArgs args, ref string grougName)
        {
            List<CustomerGroupInfo> list = new List<CustomerGroupInfo>();
            string result;

            string url = $"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get?access_token={token}";
            string parameters = JsonConvert.SerializeObject(args);
            result = HttpHelper.Submit(url, parameters);
            Root root = JsonConvert.DeserializeObject<Root>(result);
            grougName = root.group_chat.name;
            foreach (var user in root.group_chat.member_list)
            {
                CustomerGroupInfo obj = new CustomerGroupInfo();
                obj.UserID = user.userid;
                obj.GroupID = args.chat_id;
                obj.Type = user.type;
                obj.JoinTime = user.join_time;
                obj.JoinScene = user.join_scene;
                obj.InvitorUserID = string.Empty;
                if (user.invitor != null)
                    obj.InvitorUserID = user.invitor.userid;
                obj.GroupNickname = user.group_nickname;
                obj.Name = user.name;
                list.Add(obj);
            }
            return list;
        }

    }

    public class GroupChatDetailArgs
    {
        public string chat_id { set; get; }
        public int need_name { set; get; } = 1;
    }
    #region JSON
    public class Invitor
    {
        /// <summary>
        /// 
        /// </summary>
        public string userid { get; set; }
    }

    public class Member_list
    {
        /// <summary>
        /// 
        /// </summary>
        public string userid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int type { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int join_time { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int join_scene { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Invitor invitor { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string group_nickname { get; set; }
        /// <summary>
        /// 密密C
        /// </summary>
        public string name { get; set; }
    }

    public class Admin_list
    {
        /// <summary>
        /// 
        /// </summary>
        public string userid { get; set; }
    }

    public class Group_chat
    {
        /// <summary>
        /// 
        /// </summary>
        public string chat_id { get; set; }
        /// <summary>
        /// 🌸密密🌸捡漏家族C439
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int create_time { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<Member_list> member_list { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<Admin_list> admin_list { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string owner { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string member_version { get; set; }
    }

    public class Root
    {
        /// <summary>
        /// 
        /// </summary>
        public int errcode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string errmsg { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Group_chat group_chat { get; set; }
    }


    #endregion
}
